using System.Collections.Generic;
using System.Linq;
using GraphMyCode.Client.AssemblyService;
using GraphMyCode.Client.Scalling;

namespace GraphMyCode.Client.ViewModels
{
	public class GraphMapViewModel : ViewModelBase
	{
		private GraphMapDto dto;
		private bool isDraggingEnabled;
		private bool isZoomReset;
		private string searchText;
		private double zoomPercentage = 1.0;
		private readonly AbsoluteScaleEngine absoluteScaleEngine;

		public GraphMapViewModel()
		{
			absoluteScaleEngine = new AbsoluteScaleEngine();
		}

		public GraphMapViewModel(GraphMapDto dto)
		{
			this.dto = dto;
			absoluteScaleEngine = new AbsoluteScaleEngine();

			Graph = new GraphViewModel(dto.Graph, absoluteScaleEngine, dto.Metadata.Type);

			if (dto.Legend != null && dto.Legend.IsVisible)
				Legend = new LegendViewModel(dto.Legend);

			Metadata = new MetadataViewModel(dto.Metadata);
		}

		public GraphMapDto GraphMapDto
		{
			get { return dto; }
			set
			{
				if(dto == value)
					return;

				dto = value;
				Graph = new GraphViewModel(dto.Graph, absoluteScaleEngine, dto.Metadata.Type);

				if (dto.Legend != null && dto.Legend.IsVisible)
					Legend = new LegendViewModel(dto.Legend);

				Metadata = new MetadataViewModel(dto.Metadata);

				SearchText = string.Empty;
				ZoomPercentage = 1.0;
				IsDraggingEnabled = false;
			}
		}

		public string SearchText
		{
			get { return searchText; }
			set
			{
				if (searchText == value)
					return;
				searchText = value;
				OnPropertyChanged("SearchText");
			}
		}

		public LegendViewModel Legend { get; set; }

		public GraphViewModel Graph { get; private set; }

		public MetadataViewModel Metadata { get; private set; }

		public string ImageName
		{
			get
			{
				switch (Metadata.Title)
				{
					case "Assemblies":
						return "../Resources/Assembly.png";
					case "Classes":
						return "../Resources/Class1.png";
					case "Methods":
						return "../Resources/Method.png";
					default:
						return string.Empty;
				}
			}
		}

		public IEnumerable<string> AllNodesText
		{
			get { return Graph.Nodes.Select(n => n.Text); }
		}

		public double ZoomPercentage
		{
			get { return zoomPercentage; }
			set
			{
				if (zoomPercentage == value)
					return;

				zoomPercentage = value;
				OnPropertyChanged("ZoomPercentage");

				IsZoomReset = false;
			}
		}

		public bool IsZoomReset
		{
			get { return isZoomReset; }
			set
			{
				if (isZoomReset == value)
					return;

				isZoomReset = value;
				OnPropertyChanged("IsZoomReset");
			}
		}

		public bool IsDraggingEnabled
		{
			get { return isDraggingEnabled; }
			set
			{
				if (isDraggingEnabled == value)
					return;

				isDraggingEnabled = value;
				OnPropertyChanged("IsDraggingEnabled");
			}
		}
	}
}